<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Post_model extends WST_Model {
	private $_table = "post";

	function count_all($provinder_id = 0,  $category_id = 0, $need_id = 0, $type_id = 0){
		$where ="status = 1 AND locked=0 AND is_vip=0 ";
		if(intval($category_id)>0){
			$where.= " AND cat_id=".$category_id;
			if(intval($need_id)>0){
				$where.= " AND sub_cat_id =".$need_id;
			}
			if(intval($type_id)>0){
				$where.= " AND child_cat_id =".$type_id;
			}
		}
		if(intval($provinder_id)>0){
			$where.= " AND provinder_id =".$provinder_id;
		}
		$this->db->where($where);
		return $this->db->count_all_results($this->_table);
	}

	function find_list_posts($provinder_id = 0,  $category_id = 0, $need_id = 0, $type_id = 0, $page_size = 20, $page_number = 1){
    	$where =" WHERE `status` = 1 AND locked=0 AND is_vip=0 ";
		if(intval($category_id)>0){
			$where.= " AND cat_id=".$category_id;
			if(intval($need_id)>0){
				$where.= " AND sub_cat_id =".$need_id;
			}
			if(intval($type_id)>0){
				$where.= " AND child_cat_id =".$type_id;
			}
		}

		if(intval($provinder_id)>0){
			$where.= " AND provinder_id =".$provinder_id;
		}
        $sql = "select * from ".$this->_table. $where."   order by modifyTime DESC";

        if ($page_number > 0 && $page_size > 0) {
            $offset = ($page_number-1)* $page_size;
            $sql .= " limit {$offset}, {$page_size}";
        }
        $data=array();
        $query = $this->db->query($sql);
        $data=$query->result_array();
        $query->free_result();
		return $data;
    }
	function getSingle($post_id,$flag=false){
        $get_by_post_id = $this->memcached->get('get_by_post_'.$post_id);
        if($get_by_post_id===false || $flag===true){
            $this->db->where('id',$post_id);
            $query = $this->db->get($this->_table);
            $get_by_post_id= $query->row_array();
            $query->free_result();
            $this->memcached->add('get_by_post_'.$post_id, $get_by_post_id);
        }
		return $get_by_post_id;
	}


	function save($data){
		$this->db->insert($this->_table,$data);
		return $this->db->insert_id();
	}

	public function update($data,$id=null){
		$this->db->where("id",$id);
		if($this->db->update($this->_table,$data)){
            $this->getSingle($id,true);
			return $id;
        }
		else{
			return FALSE;
        }
	}
	//lay du lieu trang chu chung
	function get_home_data_vip($category_ids = array(), $num_item=20,$is_vip){
		$conditions = array();
		$conditions[] = ' `status`=1 AND locked=0 ';
		if(is_array($category_ids) && count($category_ids)>0){
    		$conditions[] = ' category_id in ('.implode($category_ids, " ,").')';
    	}
		if(!empty($is_vip)){
			$conditions[] = ' (is_vip =1)';
		}
		$sql='SELECT * FROM `post` WHERE '. implode($conditions, ' AND');
		$sql.=" order by modifyTime DESC limit ".$num_item;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	function get_home_data_post($category_ids = array(), $num_item=20){
		$conditions = array();
		$conditions[] = ' `status`=1 AND locked=0 AND show_home_page=1 AND is_vip=0 AND is_svip=0 ';
		if(is_array($category_ids) && count($category_ids)>0){
    		$conditions[] = ' category_id in ('.implode($category_ids, " ,").')';
    	}
		$sql='SELECT * FROM `post` WHERE '. implode($conditions, ' AND');
		$sql.=" order by modifyTime DESC limit ".$num_item;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	//end

	//lay trang chu theo vung
	function get_post_vip($provinder_id=100,$category_ids = array(),$num_item=20,$is_vip){
		$conditions = array();
		$conditions[] = ' provinder_id = '.$provinder_id.' AND `status`=1 AND locked=0 ';
		if(is_array($category_ids) && count($category_ids)>0){
    		$conditions[] = ' category_id in ('.implode($category_ids, " ,").')';
    	}
		if(!empty($is_vip)){
			$conditions[] = ' (is_vip =1) ';
		}

		$sql='SELECT * FROM post WHERE '. implode($conditions, ' AND');
		$sql.=" order by modifyTime DESC limit ".$num_item;
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	function get_post_home($provinder_id=100,$category_ids = array(),$num_item=20){
		$conditions = array();
		$conditions[] = ' provinder_id = '.$provinder_id.' AND `status`=1 AND locked=0 AND show_home_page=1 AND is_vip=0 AND is_svip=0 ';
		if(is_array($category_ids) && count($category_ids)>0){
    		$conditions[] = ' category_id in ('.implode($category_ids, " ,").')';
    	}
		$sql='SELECT * FROM post WHERE'. implode($conditions, ' AND');
		$sql.=" order by modifyTime desc limit ".$num_item;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	//end
	function get_post_other($params=array(),$limit){
		$data = array();
		if(count($params)){
			if(!empty($params['provinder_id'])){
				$this->db->where('provinder_id',$params['provinder_id']);
			}
			if(!empty($params['category_id'])){
				$this->db->where('category_id',$params['category_id']);
			}
			if(!empty($params['need_id'])){
				$this->db->where('need_id',$params['need_id']);
			}
			if(!empty($params['type_id'])){
				$this->db->where('type_id',$params['type_id']);
			}
			if(!empty($params['district_id'])){
				$this->db->where('district_id',$params['district_id']);
			}
			$this->db->where('id !=',$params['post_id']);
		}

		$this->db->where('status',1);
		$this->db->where('locked',0);
		$this->db->where('badword',0);
		$this->db->where('bad',0);
		$this->db->where('spam',0);

		$query =  $this->db->get($this->_table,$limit);
        $data = $query->result_array();
		$query->free_result();
		return $data;
	}

    function admin_count_all($param=array()){

        if(count($param)>0){
            if(!empty($param['keyword'])){
                $this->db->like('name',$param['keyword']);
            }
            if(!empty($param['parent'])){
                $this->db->where('cat_id', $param['parent']);
            }
            if(!empty($param['child'])){
                $this->db->where('sub_cat_id', $param['child']);
            }
            if(!empty($param['provinder'])){
                $this->db->where('provinder_id', $param['provinder']);
            }
            if(!empty($param['todate']) && !empty($param['fromdate'])){
                $this->db->where('modifyTime', ">=".strtotime($param['todate']));
                $this->db->where('modifyTime', "<=".strtotime($param['fromdate']));
            }
        }
        return $this->db->count_all_results($this->_table);
    }

    function admin_find_all($param=array(),$current_page = 1, $page_size = 20){
        $data=array();
        $where =" WHERE status=1";
        if(count($param)>0){
            if(!empty($param['keyword'])){
                $where.=" AND name LIKE '%".$param['keyword']."%'";
            }
            if(!empty($param['parent'])){
                $where.=" AND cat_id = ".$param['parent']."";
            }
            if(!empty($param['child'])){
                $where.=" AND sub_cat_id = ".$param['child']."";
            }
            if(!empty($param['provinder'])){
                $where.=' AND provinder_id ='.$param['provinder'].'';
            }
            if(!empty($param['todate']) && !empty($param['fromdate'])){
                $where.= ' AND modifyTime >='.strtotime($param['fromdate']).'';
                $where.='modifyTime <='.strtotime($param['todate']).'';
            }
        }
        $sql = "select * from ".$this->_table. $where."   order by modifyTime DESC";
        if ($current_page > 0 && $page_size > 0) {
            $offset = ($current_page-1)* $page_size;
            $sql .= " limit {$offset}, {$page_size}";
        }else {
            $sql .= " limit $page_size";
        }
        $query = $this->db->query($sql);
        $data = $query->result_array();
        $query->free_result();
        return $data;
    }

    public function member_count_all($user_id){
        $this->db->where('user_id',$user_id);
        return $this->db->count_all_results($this->_table);
    }

    public function member_find_all($user_id,$current_page = 1, $page_size = 30){
        $where = " WHERE user_id = ".$user_id;

        $sql = "select * from ".$this->_table. $where."   order by modifyTime DESC";
        if ($current_page > 0 && $page_size > 0) {
            $offset = ($current_page-1)* $page_size;
            $sql .= " limit {$offset}, {$page_size}";
        }

        $query = $this->db->query($sql);
        $data = $query->result_array();
        $query->free_result();
        return $data;
    }

    public function getFeatured($provinder_id=0,$total=10,$cat_id=0,$sub_cat_id=0,$child_cat_id=0){
        $this->db->where('provinder_id',$provinder_id);
        $this->db->where('locked',0);
        $this->db->where('status',1);
        if(!empty($cat_id)){
            $this->db->where('cat_id',$cat_id);
        }
        if(!empty($cat_id) && !empty($sub_cat_id)){
            $this->db->where('sub_cat_id',$sub_cat_id);
        }
        if(!empty($child_cat_id) && !empty($cat_id) && !empty($sub_cat_id)){
            $this->db->where('child_cat_id',$child_cat_id);
        }
        $this->db->where('cat_id !=',6);
        $this->db->limit($total);
        $this->db->order_by('modifyTime','DESC');
        $query = $this->db->get($this->_table);
        $data = $query->result_array();
        $query->free_result();
        return $data;
    }

    public function update_hit($post_id,$hit){
        $data=array();
        $data['hit']=$hit+1;
        $this->db->where('id',$post_id);
        $this->db->update($this->_table,$data);
        return true;
    }
}
